/*下拉选择器*/
jQuery.fn.select=function(option){
  
  var type = $(this).attr("type");
  var target = $(this);
  if ( "text" != type){
    console.log("该控件不支持使用此方法,请确认是否为文本输入控件");
  }
  //option = option|{};
  var x =  $(this).offset().top;
  var height = $(this).height();
  var y =  $(this).offset().left;
  //{label:"标题",value:"title"}
  var length = option.columns == undefined?0:option.columns.length;
  if ( length == 0)return;
  //var url = option.url|""; // 请求地址
  var linked = option.link;
  var data = new Object();
  if ( linked != undefined){// 关联关系设置此项会调用对应的文本框的val和name作为条件参数一起请求。
     var name = $("#"+linked).attr("name");
     data[name] = $("#"+linked).val(); 
  }
  var key = option.key;
 //  table = $(table);
  var index = 0;
  var table ="";
  var total = 0;
  var result;
 /* $(this).bind("blur",function(){ // 失去焦点关闭
	$("#select_tab").remove();
		 index=0;
		 result = null;
  });*/
  $(this).keyup(function(e){
  var code = e.keyCode; // 获取当前的按键
 if (code == 13 ){//回国键
	 var size = $("#select_tab tr").size();
	  if (size > 0 && index!=0){
	 target.val(result[index-1][key]);
	 if ($.isFunction(option.onSelect)){
		 option.onSelect(result[index-1]);
		}
	 $("#select_tab").remove();
	 index=0;
	 result = null;
	 
	 }
	  return;
  }else if ( code == 38 || code ==40){ // 上下键
	  var size = $("#select_tab tr").size();
	  if (size > 0){
		  if (total > index){
			  $(".select_td"+index).css("background","#fff8d9");
			  $(".select_td"+(index-1)).css("background","white");
			  index++;
		  }else{
			  $(".select_td"+(total-1)).css("background","white");
			  $(".select_td0").css("background","#fff8d9");
			  index=1;
		  }
		  target.val(result[index-1][key]);
	  }
	  return;
  }
  $("#select_tab").remove();
  data[target.attr("name")] = $(this).val();
     $.ajax({
	   url:option.url,
	   data:data,
	   type:'post',
	   success:function(data){
		   var obj;
		   try {
			obj = eval("("+data+")");
		} catch (e) {
			obj = data;
		}
	        total = obj.length;
		   if (total == 0)return;
		   result = obj;
		   // 组装数据
		   table = "<table id='select_tab' cellspacing='1' cellpadding='0' style='font-size:13px;background-Color:#1d6db4;z-index:3;width:"+(target.width()+50)+"px;position:absolute;left:"+y+"px;top:"+(x+height+7)+"px'><tr id='cl1' style='height:27px;line-height:27px;'>";
		  if ( option.columns[0].label!=undefined){
			  for (var i =0; i < length; i++){
				   table+="<td style='background-Color:#d7e6f5;' align='center' >"+option.columns[i].label+"</td>";
		   }
			  table+="</tr>";
		  }else{
			  table = "<table id='select_tab' cellspacing='1' cellpadding='0' style='font-size:13px;background-Color:#1d6db4;z-index:3;width:"+(target.width()+50)+"px;position:absolute;left:"+y+"px;top:"+(x+height+7)+"px'>";
		  }
		   for (var j=0;j<total;j++){
		    table+="<tr style='height:25px;line-height:25px;' class='select_tr'>";
		     for (var k=0; k < length; k++){
			   table+="<td style='background-Color:white;padding:3px 5px' align='center' class='select_td"+j+"'>"+obj[j][option.columns[k].value]+"</td>";
			 }
			 table+="</tr>";
		   }
		   table+="</table>";
		   target.parent().append(table);
		  $("#select_tab tr").each(function(m){
			   $(this).bind("click",function(){
				   if ($.isFunction(option.onSelect)){
						 option.onSelect(obj[m-1]);
						}
				   target.val(obj[m][key]);
					 $("#select_tab").remove();
					 index=0;
					 result = null;
					 return;
			   });
		   });
	   },
	   error:function(){
	     alert("连接服务器失败!");
	   }
	 });
  });
 
}
/**标签*/

jQuery.fn.label=function(option){
	// 给input button加上标签
	var arr_ = ["rgb(217, 229, 244)","rgb(67, 235, 107)","rgb(215, 210, 62)","rgb(232, 173, 143)","rgb(232, 143, 227)","rgb(225, 33, 214)","rgb(237, 119, 150)","#DC143C","#D1BBFF"
	            ,"#77DDFF","#77FF00","#BBBB00","#EE7700","#DDDDDD","#FF44AA","#FFCCCC"];
	var obj = $(this);
   $(this).find("input[type='button']").each(function(){
	   var ba = arr_[parseInt(7*Math.random())];
       $(this).css({
	"padding":"2px 20px",
	"background":ba,
	"-moz-border-radius":"8px",
	"cursor":"pointer",
    "-webkit-border-radius":"8px",
    "border-radius":"8px",
	"border":"1px solid white",
	"color":"white",
	"font":"bold 13px/25px SimSun"
	   });
	   $(this).bind("click",function(){
		   var id = $(this).parent().attr("id");
		   if ( id == obj.attr("id"))
		   {
             $("#"+option.target).append($(this));
			 if ($.isFunction(option.onSelect)){
						 option.onSelect($(this));
						}
		   }else{
		     obj.append($(this));
			 if ($.isFunction(option.onSelect)){
						 option.onSelect($(this));
						}
		   }
	       
	   });
   });
}